import VueRouter from 'vue-router'
import Vue from 'Vue'
import Home from './pages/home/Home.vue'
import UserSettings from './pages/settings/UserSettings.vue'
import UserInteraction from './pages/interaction/UserInteraction.vue'
import UserInfo from './pages/settings/UserInfo.vue'
import UserEmail from './pages/settings/UserEmail.vue'
import UserFollow from './pages/interaction/UserFollow.vue'
import UserFriend from './pages/interaction/UserFriend.vue'
import Advertisement from './pages/common/Advertisement.vue'
import FriendInfo from './pages/interaction/FriendInfo.vue'
import FollowInfo from './pages/interaction/FollowInfo.vue'
import UserDestroy from './pages/interaction/UserDestroy.vue'
import SystemNotification from './pages/message/SystemNotification.vue'
import SystemMessage from './pages/message/SystemMessage.vue'
import FriendMessage from './pages/message/FriendMessage.vue'
import GroupMessage from './pages/message/GroupMessage.vue'
import OtherMessage from './pages/message/OtherMessage.vue'

Vue.use(VueRouter)
export default new VueRouter({
    // mode:"history",
    routes: [
        {
            path: '/', component: Home,
        },
        {
            path: '/usermessage', component: SystemNotification, alias: '/notification',
            children: [
                { path: 'system-msg', component: SystemMessage, meta: { scrollToTop: false } },
                { path: 'friend-msg', component: FriendMessage, meta: { scrollToTop: true } },
                { path: 'group-msg', component: GroupMessage, meta: { scrollToTop: true } },
                { path: 'other-msg', component: OtherMessage, meta: { scrollToTop: true } }
            ]
        },
        {
            path: '/settings', component: UserSettings,
            children: [
                {
                    path: 'userinfo',
                    components: {
                        default: UserInfo,
                        adver: Advertisement
                    }
                },
                {
                    path: 'useremail',
                    components: {
                        default: UserEmail,
                        adver: Advertisement
                    }
                }
            ]
        },
        {
            path: '/interaction', component: UserInteraction,
            children: [
                {
                    path: 'userfriend', component: UserFriend,
                    //添加好友信息 为组件FriendInfo添加动态路由 
                    children: [
                        //为动态路由添加name
                        //为路径参数添加Props数据下发
                        {
                            path: 'fd/:id', name: 'fd', component: FriendInfo, props: true
                        }
                    ]
                },
                {
                    path: 'userFollow', component: UserFollow,
                    children: [
                        {
                            path: 'fw/:id', name: 'fw',
                            redirect: to => {
                                if (to.params.id % 2 === 0)
                                    return 'fd/:id'
                                else
                                    return 'fi/:id'
                            },
                            beforeEnter: (to, from, next) => {
                                console.log(from)
                                console.log(to)
                                next()
                            }
                        },
                        {
                            path: 'fd/:id', component: UserDestroy, props: true
                        },
                        {
                            path: 'fi/:id', component: FollowInfo, props: true
                        }
                    ]
                }
            ]
        }
    ],
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            const position = {}

            if(to.hash) {
                position.selector = to.hash

                if(to.hash === "#anchor") {
                    position.offset = { y: 100 }
                }
            }

            if (to.matched.some(m => m.meta.scrollToTop)) {
                position.x = 0
                position.y = 0
            }
            return position;
        }
    }
})